﻿using Newtonsoft.Json;
using System.ComponentModel;

namespace SmartMotion.Core.Tools
{
    [ReflectAble]
    public class WhileItem : ActionExpression
    {
        protected override void NextActionItemIndex(bool result)
        {
            {
                int sum = 0;//用来记录嵌套次数
                //查找所有分支，赋值MyWhile
                for (int i = CurrentIndex + 1; i < Flow.ActionItems.Count - 1; i++)
                {
                    ActionItem nextItem = Flow.ActionItems[i];
                    //查找If嵌套
                    if (nextItem.GetType() == typeof(WhileItem))
                    {
                        sum += 1; //嵌套+1
                    }
                    if (nextItem.GetType() == typeof(EndWhileItem))
                    {
                        if (sum == 0)
                        {
                            var endwhile = (EndWhileItem)nextItem;
                            endwhile.MyWhile = this;
                            break;//找到ActionEndWhile就没必要继续搜索下去了
                        }
                        sum -= 1;//嵌套-1
                    }

                    if (sum == 0)//如果没有嵌套
                    {
                        if (nextItem is BreakItem elseifItem)
                        {
                            elseifItem.MyWhile = this;
                        }
                        if (nextItem is ContinueItem elseItem)
                        {
                            elseItem.MyWhile = this;
                        }
                    }
                }
            }

            if (result) //表达式结果为True,执行While的下一句
            {
                Flow.NextIndex++;
            }
            else //表达式为false，执行Endwhile下一句
            {
                //查找EndWhile
                int sum = 0;//用来记录嵌套次数
                for (int i = CurrentIndex + 1; i < Flow.ActionItems.Count; i++)
                {
                    var nextItem = Flow.ActionItems[i];
                    //查找while嵌套
                    if (nextItem.GetType() == typeof(WhileItem))
                    {
                        sum += 1; //嵌套+1
                    }
                    if (nextItem.GetType() == typeof(EndWhileItem))
                    {
                        if (sum == 0)
                        {
                            Flow.NextIndex = i + 1;//找到EndWhile下一行
                            return;
                        }
                        sum -= 1;//嵌套-1
                    }
                }
            }
        }
    }

    [ReflectAble]
    public class ContinueItem : ActionItem
    {
        [Browsable(false), JsonIgnore]
        public WhileItem MyWhile { get; set; }

        protected override bool RunMethod()
        {
            return true;
        }

        protected override void NextActionItemIndex(bool result = true)
        {
            int sum = 0;//用来记录嵌套次数
            //查找往前找While
            for (int i = CurrentIndex - 1; i > -1; i--)
            {
                var nextItem = Flow.ActionItems[i];
                //查找If嵌套
                if (nextItem.GetType() == typeof(EndWhileItem))
                {
                    sum += 1; //嵌套+1
                }
                if (nextItem.GetType() == typeof(WhileItem))
                {
                    sum -= 1;//嵌套-1
                }
                if (sum == 0)//如果没有嵌套
                {
                    if (nextItem.GetType() == typeof(WhileItem))
                    {
                        Flow.NextIndex = i;
                        return;
                    }
                }
            }
        }
    }

    [ReflectAble]
    public class BreakItem : ActionItem
    {
        [Browsable(false), JsonIgnore]
        public WhileItem MyWhile { get; set; }

        protected override bool RunMethod()
        {
            return true;
        }

        protected override void NextActionItemIndex(bool result = true)
        {
            int sum = 0;//用来记录嵌套次数
            //查找ElseIF
            for (int i = CurrentIndex + 1; i < Flow.ActionItems.Count; i++)
            {
                var nextItem = Flow.ActionItems[i];
                //查找If嵌套
                if (nextItem.GetType() == typeof(WhileItem))
                {
                    sum += 1; //嵌套+1
                }
                if (nextItem.GetType() == typeof(EndWhileItem))
                {
                    if (sum == 0)//如果没有嵌套
                    {
                        Flow.NextIndex = i + 1;
                        return;
                    }
                    sum -= 1;//嵌套-1
                }
            }
        }
    }

    [ReflectAble]
    public class EndWhileItem : ActionItem
    {
        [Browsable(false), JsonIgnore]
        public WhileItem MyWhile { get; set; }

        protected override bool RunMethod()
        {
            return true;
        }

        protected override void NextActionItemIndex(bool result = true)
        {
            int sum = 0;//用来记录嵌套次数
            //查找往前找While
            for (int i = CurrentIndex - 1; i > -1; i--)
            {
                var nextItem = Flow.ActionItems[i];
                //查找If嵌套
                if (nextItem.GetType() == typeof(EndWhileItem))
                {
                    sum += 1; //嵌套+1
                }
                if (nextItem.GetType() == typeof(WhileItem))
                {
                    if (sum == 0) //如果没有嵌套
                    {
                        if (nextItem.GetType() == typeof(WhileItem))
                        {
                            Flow.NextIndex = i;
                            return;
                        }
                    }

                    sum -= 1;//嵌套-1
                }
                if (sum == 0)//如果没有嵌套
                {
                    if (nextItem.GetType() == typeof(WhileItem))
                    {
                        Flow.NextIndex = i;
                        return;
                    }
                }
            }
        }
    }
}